package com.lesson10.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.lesson10.model.UserModel;

@RestController
public class UserController {
	
	Map<Long, UserModel> users = new HashMap<Long, UserModel>();
	
	{
		UserModel um1 = new UserModel();
		um1.setId(1L);
		um1.setUserName("java乐园");
		um1.setPassword("111");
		users.put(um1.getId(), um1);
		
		UserModel um2 = new UserModel();
		um2.setId(2L);
		um2.setUserName("java乐园2");
		um2.setPassword("222");
		users.put(um2.getId(), um2);
	}
	
	@RequestMapping(value={"/user/getUserList"},method={RequestMethod.GET})
	public Map<String, Object> getUserList(){
		 Map<String, Object> results = new HashMap<String, Object>();
		 List<UserModel> list = new ArrayList<UserModel>();
		 Set<Long> keys = users.keySet();
		 Iterator<Long> iter = keys.iterator();
		 while(iter.hasNext()){
			 list.add(users.get(iter.next()));
		 }
		 results.put("code", "000000");
		 results.put("msg", "success");
		 results.put("body", list);
		 return results;
	}
	
	@RequestMapping("/user/getUserInfo")
	public Map<String, Object> getUserInfo(Long id){
		 Map<String, Object> results = new HashMap<String, Object>();
		 UserModel u = users.get(id);
		 if(u!=null){
			 results.put("code", "000000");
			 results.put("msg", "success");
			 results.put("body", u);
			 return results; 
		 }else{
			 results.put("code", "000001");
			 results.put("msg", "fail");
			 return results; 
		 }
	}

	@RequestMapping("/user/getUserInfoForJson")
	public Map<String, Object> getUserInfoForJson(@RequestBody UserModel um){
		 Map<String, Object> results = new HashMap<String, Object>();
		 UserModel u = users.get(um.getId());
		 if(u!=null){
			 results.put("code", "000000");
			 results.put("msg", "success");
			 results.put("body", u);
			 return results; 
		 }else{
			 results.put("code", "000001");
			 results.put("msg", "fail");
			 return results; 
		 }
	}
}
